﻿using System;
using GalaSoft.MvvmLight;

namespace Mal.WP7.TimeClock.Models
{
    public class TimeClockDayViewModel : ViewModelBase
    {
        #region Fields
        private TimeClockDay timeClockDay;

       
        //private DateTime day;

        //private DateTime? beginMorning;
        //private DateTime? endMorning;
        //private DateTime? beginAfternoon;
        //private DateTime? endAfternoon;
        private TimeSpan? morningTime;
        private TimeSpan? afternoonTime;
        private TimeSpan? dayTime;

        #endregion

        #region Construction / Destruction
        public TimeClockDayViewModel(TimeClockDay timeClockDay)
        {
            this.timeClockDay = timeClockDay;
        }        
        #endregion

        #region enum, struct, class

        #endregion

        #region Properties
        public TimeClockDay TimeClockDay
        {
            get { return timeClockDay; }

            private set { timeClockDay = value; }
        }

        public DateTime Day
        {
            get
            {
                return this.timeClockDay.Day;
            }

            private set
            {
                if (this.timeClockDay.Day != value)
                {
                    this.timeClockDay.Day = value;
                    this.RaisePropertyChanged("Day");
                }
            }
        }

        public DateTime? BeginMorning
        {
            get
            {
                return this.timeClockDay.BeginMorning;
            }

            set
            {
                this.timeClockDay.BeginMorning = value;
                this.CalculDuration();
                this.RaisePropertyChanged("BeginMorning");
            }
        }

        public DateTime? EndMorning
        {
            get
            {
                return this.timeClockDay.EndMorning;
            }

            set
            {
                this.timeClockDay.EndMorning = value;
                this.CalculDuration();
                this.RaisePropertyChanged("EndMorning");
            }
        }

        public DateTime? BeginAfternoon
        {
            get
            {
                return this.timeClockDay.BeginAfternoon;
            }

            set
            {
                this.timeClockDay.BeginAfternoon = value;
                this.CalculDuration();
                this.RaisePropertyChanged("BeginAfternoon");
            }
        }

        public DateTime? EndAfternoon
        {
            get
            {
                return this.timeClockDay.EndAfternoon;
            }

            set
            {
                this.timeClockDay.EndAfternoon = value;
                this.CalculDuration();
                this.RaisePropertyChanged("EndAfternoon");
            }
        }

        public TimeSpan? MorningTime
        {
            get
            {
                return this.morningTime;
            }

            private set
            {
                this.morningTime = value;
                this.RaisePropertyChanged("MorningTime");
            }
        }

        public TimeSpan? AfternoonTime
        {
            get
            {
                return this.afternoonTime;
            }

            private set
            {
                this.afternoonTime = value;
                this.RaisePropertyChanged("AfternoonTime");
            }
        }

        public TimeSpan? DayTime
        {
            get
            {
                return this.dayTime;
            }

            set
            {
                this.dayTime = value;
                this.RaisePropertyChanged("DayTime");
            }
        }
        #endregion

        #region Methods
        private void CalculDuration()
        {
            if (this.BeginMorning.HasValue)
            {
                this.Day = this.BeginMorning.Value.Date;
            }
            else if (this.BeginAfternoon.HasValue)
            {
                this.Day = this.BeginAfternoon.Value.Date;
            }

            if (this.timeClockDay.BeginMorning.HasValue && this.timeClockDay.EndMorning.HasValue)
            {
                this.morningTime = this.timeClockDay.EndMorning.Value.Subtract(this.timeClockDay.BeginMorning.Value);
            }

            if (this.timeClockDay.BeginAfternoon.HasValue && this.timeClockDay.EndAfternoon.HasValue)
            {
                this.AfternoonTime = this.timeClockDay.EndAfternoon.Value.Subtract(this.timeClockDay.BeginAfternoon.Value);
            }

            this.DayTime = null;

            if (this.MorningTime.HasValue)
            {
                this.DayTime = this.MorningTime;
            }

            if (this.AfternoonTime.HasValue)
            {
                if (this.DayTime.HasValue)
                {
                    this.DayTime = this.DayTime.Value.Add(this.AfternoonTime.Value);
                }
                else
                {
                    this.DayTime = this.AfternoonTime;
                }
            }
        }
        #endregion

        #region Events

        #endregion
    }
}
